// library shared_components;

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_components/utils/index.dart';

Widget buildInput(
    {String title = '名称',
    bool required = false,
    bool hiddend = false,
    bool disabled = false,
    String direction = "ltr",
    Widget? titleAfterSlot,
    String unit = '',
    String before = '',
    FocusNode? focusNode,
    String type = 'text',
    String value = '',
    int maxLines = 1,
    Widget? unitSolt,
    TextEditingController? textEditingController,
    List<TextInputFormatter>? inputFormatters,
    double labelWidth = 90,
    double height = 60,
    double labelHeight = 60,
    String placeholder = '请填写',
    Function? onChanged}) {
  // var myTextEditingController = null;
  var myTextEditingController = textEditingController;
  if (textEditingController != null) {
    myTextEditingController = textEditingController;
  }
  // debounceFunc(() {
  // myTextEditingController.text = value;
  // }, 800);

  void _moveCursorToEnd() {
    // 将光标移动到文本末尾
    myTextEditingController?.selection =
        TextSelection.fromPosition(TextPosition(
      affinity: TextAffinity.downstream,
      offset: myTextEditingController.text.length,
    ));
  }

  _moveCursorToEnd();

  Widget getLabel({String direction = "ltr"}) {
    return Container(
      width: direction == "ttb" ? double.infinity : labelWidth,
      alignment: Alignment.centerLeft,
      height: direction == "ttb" ? labelHeight : double.infinity,
      // color: Colors.yellow,
      child: Row(
        children: [
          Visibility(
              visible: required,
              child: const Padding(
                padding: EdgeInsets.only(top: 5),
                child: Text(
                  "*",
                  style: TextStyle(fontSize: 20, color: Colors.red),
                ),
              )),
          Text(
            title,
            style: TextStyle(fontSize: 16),
          ),
          Expanded(
              child: Container(
            width: double.infinity,
            height: double.infinity,
            // color: Colors.red,
            child: titleAfterSlot,
          ))
        ],
      ),
    );
  }

  Widget getInput() {
    return Container(
        // color: Colors.red,
        child: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        //输入框
        Visibility(
            visible: type == 'text',
            child: CupertinoTextFormFieldRow(
              focusNode: focusNode,
              controller: myTextEditingController,
              enabled: !disabled,
              textAlign: TextAlign.right,
              inputFormatters: inputFormatters,
              decoration: const BoxDecoration(color: Colors.transparent),
              // autofocus: true,
              placeholder: placeholder,
              placeholderStyle: TextStyle(
                  color: CupertinoColors.darkBackgroundGray.withOpacity(.7)),
              keyboardAppearance: Brightness.dark,
              // keyboardType: TextInputType.emailAddress,
              onChanged: (value) {
                if (onChanged != null) {
                  // debounceFunc(() {
                  onChanged(value);
                  // }, 400);
                }
              },
            )),
        Visibility(
          visible: type == 'textArea',
          child: TextField(
            enabled: !disabled,
            focusNode: focusNode,
            controller: myTextEditingController,
            textAlign: TextAlign.left,
            maxLines: maxLines,
            keyboardAppearance: Brightness.dark,
            onChanged: (value) {
              if (onChanged != null) {
                // debounceFunc(() {
                onChanged(value);
                // }, 400);
              }
            },
            inputFormatters: inputFormatters,
            //  [FilteringTextInputFormatter.deny(RegExp(','))],
            decoration: InputDecoration(
              border: OutlineInputBorder(),
              hintText: placeholder,
            ),
          ),
        )
      ],
    ));
  }

  if (direction == "ttb") {
    return Visibility(
        visible: !hiddend,
        child: Container(
          width: double.infinity,
          height: height,
          padding: EdgeInsets.only(left: 10, right: 10),
          // margin: const EdgeInsets.only(left: 15, right: 15),
          decoration: const BoxDecoration(
            border: Border(
                top: BorderSide(width: 1.0, color: Color(0xffF4F4F4)),
                bottom: BorderSide(width: 1.0, color: Color(0xffF4F4F4))),
          ),
          child: Column(
            children: [
              getLabel(direction: direction),
              Expanded(flex: 1, child: getInput())
            ],
          ),
        ));
  }
  return Visibility(
      visible: !hiddend,
      child: Container(
        width: double.infinity,
        height: height,
        padding: EdgeInsets.only(left: 10, right: unit == '' ? 10 : 0),
        // margin: const EdgeInsets.only(left: 15, right: 15),
        decoration: const BoxDecoration(
          // color: Colors.green,
          border: Border(
              top: BorderSide(width: 1.0, color: Color(0xffF4F4F4)),
              bottom: BorderSide(width: 1.0, color: Color(0xffF4F4F4))),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(flex: 1, child: getLabel()),
            Visibility(
                visible: before != "",
                child: Text(
                  before,
                  style: TextStyle(
                    fontSize: 14,
                    // color: Colors.white
                  ),
                )),
            Expanded(
                flex: 2,
                child: Container(
                  // color: Colors.blue,
                  child: getInput(),
                )),
            Visibility(
                visible: unit != "",
                child: Container(
                  width: 40,
                  // color: Colors.red,
                  // padding: EdgeInsets.only(left: 5),
                  height: double.infinity,
                  // color: Color(0xff6c717c),
                  alignment: Alignment.center,
                  child: unitSolt ??
                      Text(
                        unit,
                        style: TextStyle(
                          fontSize: 14,
                          // color: Colors.white
                        ),
                      ),
                ))
          ],
        ),
      ));
}
